package utwente.hmi.touchmol;

import java.awt.Color;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Observable;
import java.util.Properties;

/**
 * @author Jeroen Logtenberg
 */
public class Configuration extends Observable {
	
	private Properties pp;
	private static Configuration me;
	
	/**
	 * Constructor, loads the configuration file.
	 */
	public Configuration() {
		pp = new Properties();
		me = this;
		load(new File("TouchMol.xml"));
	}
	
	public static Configuration getInstance() {
		return me;
	}
	
	public String getConfigValue(String name, String def) {
		String s = pp.getProperty(name);
		if(s == null) {
			TouchMolGUI.debug("[Config] Value '"+name+"' not set, using default: "+def);
			return def;
		} else {
			return s;
		}	
	}
	
	public String getConfigValue(String name) {
		return getConfigValue(name, "0");
	}
	
	public double getConfigDouble(String name) {
		return getConfigDouble(name,0.0);
	}
	
	public double getConfigDouble(String name, double def) {
		String s = getConfigValue(name,""+def);
		try {
			double d = Double.parseDouble(s);
			return d;
		} catch(NumberFormatException e) {
			TouchMolGUI.debug("[Config] Double value '"+name+"' not valid, saw '"+s+"'");
			return def;
		}
	}
	
	public int getConfigInteger(String name) {
		return getConfigInteger(name, 0);
	}
	
	public int getConfigInteger(String name, int def) {
		String s = getConfigValue(name,""+def);
		try {
			int d = Integer.parseInt(s);
			return d;
		} catch(NumberFormatException e) {
			TouchMolGUI.debug("[Config] Integer value '"+name+"' not valid.");
			return def;
		}
	}
	
	public boolean getConfigBoolean(String name) {
		return getConfigBoolean(name, false);
	}
	
	public boolean getConfigBoolean(String name, boolean def) {
		String s = getConfigValue(name,""+def);
		
		return s.equals("true");
	}
	
	public Color getConfigColor(String name) {
		return getConfigColor(name,Color.WHITE);
	}
	
	public Color getConfigColor(String name, Color def) {
		String s = getConfigValue(name,""+def);
		try {
			Color c = Color.decode(s);
			return c;
		} catch(NumberFormatException e) {
			TouchMolGUI.debug("[Config] Color value '"+name+"' not valid.");
			return def;
		}
	}
	
	public Properties getProperties() {
		return pp;
	}
	
	/**
	 * setConfigValue saves the configuration values to a properties format
	 * @param name The name of the config value
	 * @param value Value of the key
	 */
	public void setConfigValue(String name, String value) {
		pp.setProperty(name, value);
		setChanged();
    	notifyObservers();
	}
	
	/**
	 * This method loads an XML file and put the contents in Properties
	 * @param f The file to load
	 */
	public void load(File f) {
		FileInputStream is;
		try { 
			is = new FileInputStream(f);
			pp.loadFromXML(is);
		} catch(FileNotFoundException e) {
			TouchMolGUI.debug("Loading configuration: FileNotFound " + e.getMessage());
		} catch(IOException e) {
			TouchMolGUI.debug("Loading configuration: IOException " + e.getMessage());
			e.printStackTrace();
		}
		setChanged();
		notifyObservers();
	}
	
	/**
	 * This method saves the configuration options to an XML file
	 * @param f File to save
	 */
	public void save(File f) {
		FileOutputStream fo;
		try {
			fo = new FileOutputStream(f);
			pp.storeToXML(fo, "TouchMol config file");
		} catch(FileNotFoundException e) {
			TouchMolGUI.debug("Saving: FileNotFound" + e.getMessage());
		} catch(IOException e) {
			TouchMolGUI.debug("Saving: IOException" + e.getMessage());
		}
		
	}
}